﻿using Service.Framework.OperationLog;
using Service.Framework.Utils.Helper;
using Service.Repository;
using SqlSugar;
namespace Service.Core.Repository.Core
{

    /// <summary>
    /// 日志表
    /// </summary>

    [SugarIndex("log_type_inx", nameof(Type), OrderByType.Desc)]
    [SugarIndex("log_user_id_inx", nameof(UserId), OrderByType.Desc)]
    [SplitTable(SplitType.Year)]
    [SugarTable("tb_operation_log_{year}{month}{day}")]
    [Tenant(DbTenantEnum.Log)]
    public class OperationLog : Entity, IAggregateRoot
    {
        public OperationLog()
        {
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="apiMethod"></param>
        /// <param name="apiPath"></param>
        /// <param name="elapsedMilliseconds"></param>
        /// <param name="params"></param>
        /// <param name="result"></param>
        /// <param name="browserInfo"></param>
        /// <param name="ip"></param>
        /// <param name="name"></param>
        public OperationLog(string msg, string apiMethod, string apiPath, long elapsedMilliseconds, string @params, string result, string browserInfo, string ip, string name, OperationLogType type, string userId, string clientId, string devicePlatform = "", string deviceId = "", string version = "", string backupField1 = "", string backupField2 = "", string backupField3 = "")
        {
            Id = SnowflakeUtil.Instance().GetId();
            Msg = msg;
            ApiMethod = apiMethod;
            ApiPath = apiPath;
            ElapsedMilliseconds = elapsedMilliseconds;
            Params = @params;
            Result = result;
            BrowserInfo = browserInfo;
            Ip = ip;
            Name = name;
            CreateTime = DateTime.Now;
            Type = type;
            UserId = userId;
            ClientId = clientId;
            DevicePlatform = devicePlatform;
            DeviceId = deviceId;
            Version = version;
            BackupField1 = backupField1;
            BackupField2 = backupField2;
            BackupField3 = backupField3;
        }

        [SugarColumn(ColumnName = "id", IsPrimaryKey = true)]
        public long Id { get; private set; }

        /// <summary>
        /// 日志内容
        /// </summary>
        [SugarColumn(ColumnName = "msg", ColumnDataType = "text")]
        public string Msg { get; private set; }

        /// <summary>
        /// 请求方法
        /// </summary>
        [SugarColumn(ColumnName = "api_method")]
        public string ApiMethod { get; private set; }

        /// <summary>
        /// 请求地址
        /// </summary>
        [SugarColumn(ColumnName = "api_path")]
        public string ApiPath { get; private set; }

        /// <summary>
        /// 执行耗时
        /// </summary>
        [SugarColumn(ColumnName = "elapsed_Milliseconds")]
        public long ElapsedMilliseconds { get; private set; }

        /// <summary>
        /// 参数
        /// </summary>
        [SugarColumn(ColumnName = "params", ColumnDataType = "text")]
        public string Params { get; private set; }

        /// <summary>
        /// 返回结果
        /// </summary>
        [SugarColumn(ColumnName = "result", ColumnDataType = "text")]
        public string Result { get; private set; }

        /// <summary>
        /// 
        /// </summary>
        [SugarColumn(ColumnName = "browserInfo", Length = 512, IsNullable = true)]
        public string BrowserInfo { get; private set; }

        /// <summary>
        /// 请求ip
        /// </summary>
        [SugarColumn(ColumnName = "ip")]
        public string Ip { get; private set; }

        /// <summary>
        /// 日志名称
        /// </summary>
        [SugarColumn(ColumnName = "name")]
        public string Name { get; private set; }


        /// <summary>
        /// 日志类型
        /// </summary>
        [SugarColumn(ColumnName = "type")]
        public OperationLogType Type { get; private set; }

        /// <summary>
        /// 日志创建时间
        /// </summary>
        [SplitField]
        [SugarColumn(ColumnName = "create_time")]
        public DateTime CreateTime { get; private set; }

        /// <summary>
        /// 操作人
        /// </summary>
        [SugarColumn(ColumnName = "user_id", IsNullable = true)]
        public string? UserId { get; private set; }


        /// <summary>
        /// 操作人客户端
        /// </summary>
        [SugarColumn(ColumnName = "client_id", IsNullable = true)]
        public string? ClientId { get; private set; }

        /// <summary>
        /// 操作人系统
        /// </summary>
        [SugarColumn(ColumnName = "device_platform", IsNullable = true)]
        public string DevicePlatform { get; private set; }

        /// <summary>
        /// 操作人设备标识
        /// </summary>
        [SugarColumn(ColumnName = "deviceId", IsNullable = true)]
        public string DeviceId { get; private set; }

        /// <summary>
        /// 操作人客户端版本
        /// </summary>
        [SugarColumn(ColumnName = "version", IsNullable = true)]
        public string Version { get; private set; }

        [SugarColumn(ColumnName = "backup_field1", IsNullable = true)]
        public string? BackupField1 { get; set; }

        [SugarColumn(ColumnName = "backup_field2", IsNullable = true)]
        public string BackupField2 { get; set; }

        [SugarColumn(ColumnName = "backup_field3", IsNullable = true)]
        public string BackupField3 { get; set; }
    }
}
